<%@page import="java.sql.ResultSet"%>
<%@page contentType="text/html" pageEncoding="UTF-8" errorPage="../error.jsp"%>
<%@ page import="java.sql.*" %>
<%@page import="df.jsp.Guard"%>
<%@page import="df.jsp.LabelMap"%>
<%@page import="df.bean.db.DBMgr"%>

<%@page import="df.bean.db.DataRecord"%>
<%@page import="df.bean.obj.util.JDate"%>

<%@page import="java.sql.Types"%>

<%@ include file="../../_global.jsp" %>

<%
            // Short Page Profile Doctor
            // Verify permission
            //

            if (!Guard.checkPermission(session, Guard.PAGE_INPUT_DOCTOR)) {
                response.sendRedirect("../message.jsp");
                return;
            }

            //
            // Initial LabelMap
            //
            
            request.setCharacterEncoding("UTF-8");

            if (session.getAttribute("LANG_CODE") == null) {
                session.setAttribute("LANG_CODE", LabelMap.LANG_EN);
            }
            LabelMap labelMap = new LabelMap(session.getAttribute("LANG_CODE").toString());
            labelMap.add("TITLE_MAIN", "Doctor Details", "รายละเอียดข้อมูลแพทย์");
            labelMap.add("HOSPITAL_CODE", "Hospital Code", "รหัสโรงพยาบาล");
            labelMap.add("DOCTOR_PROFILE_CODE", "Profile Code", "รหัสประวัติแพทย์");
            labelMap.add("CODE", "Doctor Code", "รหัสแพทย์");
            labelMap.add("NAME_THAI", "Name (TH)", "ชื่อ (ไทย)");
            labelMap.add("NAME_ENG", "Name (EN)", "ชื่อ (อังกฤษ)");
            labelMap.add("TAX_ID", "Tax ID / Nation ID", "เลขผู้เสียภาษี/เลขบัตรประชาชน");
            labelMap.add("LICENSE_ID", "License ID", "เลขที่ใบอนุญาต");
            labelMap.add("FROM_DATE", "License Issue Date", "วันออกใบอนุญาต");
            labelMap.add("TO_DATE", "License Expire Date", "วันหมดอายุ");

            labelMap.add("SUBTITLE_ADDRESS", "Address", "ที่อยู่");
            labelMap.add("ADDRESS1", "Address No. / Address", "เลขที่/ที่อยู่");
            labelMap.add("ADDRESS2", "Sub District / District", "แขวง/เขต");
            labelMap.add("ADDRESS3", "Province", "จังหวัด");
            labelMap.add("ZIP", "Postal Code", "รหัสไปรษณีย์");

            labelMap.add("SUBTITLE_INFORMATION", "Doctor Information", "ข้อมูลแพทย์");
            labelMap.add("DOCTOR_TYPE_CODE", "Doctor Type", "ประเภทแพทย์");
            labelMap.add("SUB_SPECIALTY", "Sub-Specialty", "วิชาชีพเฉพาะทาง");
            labelMap.add("SUB_SPECIALTY_SF", "Sub Field", "Sub Field");
            labelMap.add("SUB_SPECIALTY_SB", "Sub Board", "Sub Board");
            labelMap.add("SUB_SPECIALTY_SC", "Special Clinic", "Special Clinic");
            labelMap.add("SUB_SPECIALTY_DN", "Dental", "Dental");
            labelMap.add("SUB_SPECIALTY_GP", "General Practice", "General Practice");
            labelMap.add("SUB_SPECIALTY_DT", "DT02", "DT02");
            labelMap.add("SUB_SPECIALTY_XR", "X Ray", "X Ray");

            labelMap.add("MAJOR1", "Major1", "Major1");
            labelMap.add("MAJOR2", "Major2", "Major2");
            labelMap.add("MAJOR3", "Major3", "Major3");
            labelMap.add("UNDEFINED", "--- Undefined ---", "--- ไม่ระบุ ---");

            labelMap.add("DOCTOR_CATEGORY_CODE", "Category Allocate", "กลุ่มเงื่อนไขส่วนแบ่งแพทย์");
            labelMap.add("HOSPITAL_UNIT_CODE", "Business Unit", "หน่วยธุรกิจ");
            labelMap.add("DEPARTMENT_CODE", "Department Code", "รหัสแผนก");
            labelMap.add("PAYMENT_MODE_CODE", "Payment Mode", "ประเภทการจ่าย");
            labelMap.add("PAY_TAX_402_BY", "Pay Tax by", "จ่ายภาษีโดย");
            labelMap.add("ABSORB_DR_CODE", "Absorb Doctor Code", "รายได้เติมเต็มการันตีที่รหัสแพทย์");
            labelMap.add("GUARANTEE_DR_CODE", "Guarantee with Code", "การันตีรวมที่แพทย์รหัส");
            labelMap.add("GUARANTEE_DR_CODE_DP", "Doctor Profile", "รหัสประวัติแพทย์");
            labelMap.add("GUARANTEE_DR_CODE_D", "Doctor", "รหัสแพทย์");
            labelMap.add("GUARANTEE_SOURCE", "Revenue for Guarantee", "คิดการันตีจากยอด");
            labelMap.add("GUARANTEE_SOURCE_DF", "DF After Allocate", "จำนวนเงินหลังแบ่ง");
            labelMap.add("GUARANTEE_SOURCE_DF_VALUE", "DF", "DF");
            labelMap.add("GUARANTEE_SOURCE_TAX", "DF Before Allocate", "จำนวนเงินก่อนแบ่ง");
            labelMap.add("GUARANTEE_SOURCE_TAX_VALUE", "FULL", "FULL");
            labelMap.add("GUARANTEE_PROFILE", "Guarantee In Profile", "การันตีภายใต้แพทย์เจ้าของ");
            labelMap.add("GUARANTEE_DAY", "Guarantee by Day", "วันที่นำมาคิดการันตี");
            labelMap.add("GUARANTEE_DAY_VERIFY", "Execute Date", "วันที่ทำการคีย์ค่าแพทย์");
            labelMap.add("GUARANTEE_DAY_VERIFY_VALUE", "VER", "VER");
            labelMap.add("GUARANTEE_DAY_INVOICE", "Invoice Date", "วันที่ทำการออกใบแจ้งหนี้");
            labelMap.add("GUARANTEE_DAY_INVOICE_VALUE", "INV", "INV");
            labelMap.add("GUARANTEE_DAY_UNDEFINE", "Undefine Date", "ไม่ทำการกำหนด");
            labelMap.add("SALARY", "Salary", "เงินเดือน");
            labelMap.add("POSITION_AMT", "Position Amount", "ค่าประจำตำแหน่ง");
            labelMap.add("OVER_GUARANTEE_PCT", "Over Guarantee Allocate", "ส่วนแบ่งเกินการันตี");
            labelMap.add("IN_GUARANTEE_PCT", "In Guarantee Allocate", "ส่วนแบ่งในการันตี");
            labelMap.add("IS_HOLD", "Hold Payment", "ระงับการจ่ายค่าแพทย์");
            labelMap.add("IS_HOLD_Y", "Yes", "ใช่");
            labelMap.add("IS_HOLD_N", "No", "ไม่ใช่");
            labelMap.add("TIME_TABLE", "Time Table", "ตารางข้อมูลแพทย์");
            labelMap.add("IS_ADVANCE_PAYMENT", "Advance Payment", "จ่ายค่าแพทย์ทุกกรณี");
            labelMap.add("IS_ADVANCE_PAYMENT_Y", "Yes", "ใช่");
            labelMap.add("IS_ADVANCE_PAYMENT_N", "No", "ไม่ใช่");
            labelMap.add("GUARANTEE_START_DATE","Guarantee Start Date","วันเริ่มการันตี");
            labelMap.add("GUARANTEE_EXPIRE_DATE","Guarantee Expire Date","วันสิ้นสุดการันตี");
			labelMap.add("SUBTITLE_PAYMENT_INFORMATION", "Payment/Tax Information", "ข้อมูลการจ่ายเงินและภาษี");
            labelMap.add("SUBTITLE_BANK_ACCOUNT_INFORMATION", "Bank Account Information", "ข้อมูลบัญชีธนาคาร");
            labelMap.add("BANK_ACCOUNT_NO", "Account No", "เลขที่บัญชี");
            labelMap.add("BANK_ACCOUNT_NAME", "Account Name", "ชื่อบัญชี");
            labelMap.add("BANK_CODE", "Bank Code", "รหัสธนาคาร");
            labelMap.add("BANK_BRANCH_CODE", "Branch Code", "รหัสสาขา");
            labelMap.add("GUARANTEE_INFORMATION", "Guarantee Information", "รายละเอียดการการันตี");
            labelMap.add("EMAIL", "E-mail", "E-mail");
            labelMap.add("SUBTITLE_OTHER", "Other", "อื่น ๆ");
            labelMap.add("NOTE", "Note", "หมายเหตุ");
            labelMap.add("taxG_d","Doctor","แพทย์");
            labelMap.add("taxG_h","Hospital","โรงพยาบาล");
            labelMap.add("ALERT_BANK","Plase select BANK","กรุณาเลือกธนาคาร");
            request.setAttribute("labelMap", labelMap.getHashMap());

            String[] taxGT = {labelMap.get("taxG_d"),labelMap.get("taxG_h")};
            String[] taxValue = {"0","1"};

      
		
            DataRecord doctorRec = null, doctorProfileRec = null, doctorCategoryRec = null, bankRec = null, bankBranchRec = null, departmentRec = null, hospitalUnitRec = null;
            DataRecord doctorRecLog = null;
            byte MODE = DBMgr.MODE_INSERT;
                    
            if (request.getParameter("MODE") != null) {

                MODE = Byte.parseByte(request.getParameter("MODE"));

                doctorRec = new DataRecord("DOCTOR");
                doctorRecLog = new DataRecord("LOG_DOCTOR");

                if(true){
                	//out.println(request.getParameter("IS_HOLD"));
                	//return;
                }
                doctorRec.addField("HOSPITAL_CODE", Types.VARCHAR, session.getAttribute("HOSPITAL_CODE").toString(), true);
                doctorRec.addField("DOCTOR_PROFILE_CODE", Types.VARCHAR, request.getParameter("DOCTOR_PROFILE_CODE"));
                doctorRec.addField("CODE", Types.VARCHAR, request.getParameter("CODE"), true);
                doctorRec.addField("NAME_THAI", Types.VARCHAR, request.getParameter("NAME_THAI"));
                doctorRec.addField("NAME_ENG", Types.VARCHAR, request.getParameter("NAME_ENG"));
                doctorRec.addField("ADDRESS1", Types.VARCHAR, request.getParameter("ADDRESS1"));
                doctorRec.addField("ADDRESS2", Types.VARCHAR, request.getParameter("ADDRESS2"));
                doctorRec.addField("ADDRESS3", Types.VARCHAR, request.getParameter("ADDRESS3"));
                doctorRec.addField("ZIP", Types.VARCHAR, request.getParameter("ZIP"));
                String strGuranteeDrCode = request.getParameter("CODE");
                doctorRec.addField("EMAIL", Types.VARCHAR, request.getParameter("EMAIL"));
                doctorRec.addField("UPDATE_DATE", Types.VARCHAR, JDate.getDate());
                doctorRec.addField("UPDATE_TIME", Types.VARCHAR, JDate.getTime());
                doctorRec.addField("USER_ID", Types.VARCHAR, session.getAttribute("USER_ID").toString());

                // for log
                doctorRecLog.addField("HOSPITAL_CODE", Types.VARCHAR, session.getAttribute("HOSPITAL_CODE").toString(), true);
                doctorRecLog.addField("DOCTOR_PROFILE_CODE", Types.VARCHAR, request.getParameter("DOCTOR_PROFILE_CODE"));
                doctorRecLog.addField("CODE", Types.VARCHAR, request.getParameter("CODE"), true);
                doctorRecLog.addField("NAME_THAI", Types.VARCHAR, request.getParameter("NAME_THAI"));
                doctorRecLog.addField("NAME_ENG", Types.VARCHAR, request.getParameter("NAME_ENG"));
                doctorRecLog.addField("ADDRESS1", Types.VARCHAR, request.getParameter("ADDRESS1"));
                doctorRecLog.addField("ADDRESS2", Types.VARCHAR, request.getParameter("ADDRESS2"));
                doctorRecLog.addField("ADDRESS3", Types.VARCHAR, request.getParameter("ADDRESS3"));
                doctorRecLog.addField("ZIP", Types.VARCHAR, request.getParameter("ZIP"));
                doctorRecLog.addField("EMAIL", Types.VARCHAR, request.getParameter("EMAIL"));
                doctorRecLog.addField("UPDATE_DATE", Types.VARCHAR, JDate.getDate());
                doctorRecLog.addField("UPDATE_TIME", Types.VARCHAR, JDate.getTime());
                doctorRecLog.addField("USER_ID", Types.VARCHAR, session.getAttribute("USER_ID").toString());
                
                if (MODE == DBMgr.MODE_INSERT) {
                	//doctorRec.addField("ACTIVE", Types.VARCHAR, request.getParameter("ACTIVE"));
                	//doctorRecLog.addField("ACTIVE", Types.VARCHAR, request.getParameter("ACTIVE"));
                    if (DBMgr.insertRecord(doctorRec)) {
                        session.setAttribute("MSG", labelMap.get(LabelMap.MSG_SAVE_SUCCESS).replace("[HREF]", "input/doctor_mao_setup.jsp?CODE=" + doctorRec.getField("DOCTOR_PROFILE_CODE").getValue()));
                    } 
                    else {
                        session.setAttribute("MSG", labelMap.get(LabelMap.MSG_SAVE_FAIL));
                    }
                }else if (MODE == DBMgr.MODE_UPDATE) {
                	Boolean act = false;
                	//if(1==1){
//                 		if(!request.getParameter("BANK_ACCOUNT_NO").equalsIgnoreCase(request.getParameter("TEMP_BANK_ACCOUNT_NO"))){
//                 			act = true;
//                 		}else if(!request.getParameter("BANK_ACCOUNT_NAME").equalsIgnoreCase(request.getParameter("TEMP_BANK_ACCOUNT_NAME"))){
//                 			act = true;
//                 		}else if(!request.getParameter("BANK_CODE").equalsIgnoreCase(request.getParameter("TEMP_BANK_CODE"))){
//                 			act = true;
//                 		}else if(!request.getParameter("BANK_BRANCH_CODE").equalsIgnoreCase(request.getParameter("TEMP_BANK_BRANCH_CODE"))){
//                 			act = true;
//                 		}
//                 		if(act || request.getParameter("ACTIVE").equals(null)){
//                 			//out.println("Active = '';");
//                 			doctorRecLog.addField("ACTIVE", Types.VARCHAR, "");
//                 			doctorRec.addField("ACTIVE", Types.VARCHAR, "");
//                 		}else{
//                 			doctorRecLog.addField("ACTIVE", Types.VARCHAR, request.getParameter("ACTIVE"));
//                 			doctorRec.addField("ACTIVE", Types.VARCHAR, request.getParameter("ACTIVE"));
//                 		}
                		//return;
                	//}                	
                	
                    DBMgr.insertRecord(doctorRecLog);
                    if (DBMgr.updateRecord(doctorRec)) {
                        session.setAttribute("MSG", labelMap.get(LabelMap.MSG_SAVE_SUCCESS).replace("[HREF]", "input/doctor_mao_setup.jsp?CODE=" + doctorRec.getField("DOCTOR_PROFILE_CODE").getValue()));
                    } 
                    else {
                        session.setAttribute("MSG", labelMap.get(LabelMap.MSG_SAVE_FAIL));
                    }
                }
                
                response.sendRedirect("../message.jsp");
                return;
            }
            else if (request.getParameter("DOCTOR_PROFILE_CODE") != null && request.getParameter("CODE") != null) {
                doctorRec = DBMgr.getRecord("SELECT * FROM DOCTOR WHERE CODE = '" + request.getParameter("CODE") + "' AND DOCTOR_PROFILE_CODE = '" + request.getParameter("DOCTOR_PROFILE_CODE") + "' AND HOSPITAL_CODE='" + session.getAttribute("HOSPITAL_CODE") + "' " );
                doctorProfileRec = DBMgr.getRecord("SELECT CODE, NAME_" + labelMap.getFieldLangSuffix() + " AS NAME FROM DOCTOR_PROFILE WHERE CODE = '" + request.getParameter("DOCTOR_PROFILE_CODE") + "' AND HOSPITAL_CODE='" + session.getAttribute("HOSPITAL_CODE") + "' ");
                if (doctorRec == null || doctorProfileRec == null) {
                    MODE = DBMgr.MODE_INSERT;
                }
                else {
                    MODE = DBMgr.MODE_UPDATE;
                   // doctorCategoryRec = DBMgr.getRecord("SELECT CODE, DESCRIPTION FROM DOCTOR_CATEGORY WHERE CODE = '" + DBMgr.getRecordValue(doctorRec, "DOCTOR_CATEGORY_CODE") + "'");
                   // bankRec = DBMgr.getRecord("SELECT CODE, DESCRIPTION_" + labelMap.getFieldLangSuffix() + " AS DESCRIPTION FROM BANK WHERE CODE = '" + DBMgr.getRecordValue(doctorRec, "BANK_CODE") + "'");
                   // bankBranchRec = DBMgr.getRecord("SELECT CODE, DESCRIPTION_" + labelMap.getFieldLangSuffix() + " AS DESCRIPTION FROM BANK_BRANCH WHERE CODE = '" + DBMgr.getRecordValue(doctorRec, "BANK_BRANCH_CODE") + "' AND BANK_CODE='"+DBMgr.getRecordValue(doctorRec, "BANK_CODE")+"'");
                   // departmentRec = DBMgr.getRecord("SELECT CODE, DESCRIPTION FROM DEPARTMENT WHERE CODE = '" + DBMgr.getRecordValue(doctorRec, "DEPARTMENT_CODE") + "'");
                   // hospitalUnitRec = DBMgr.getRecord("SELECT CODE, DESCRIPTION FROM HOSPITAL_UNIT WHERE CODE = '" + DBMgr.getRecordValue(doctorRec, "HOSPITAL_UNIT_CODE") + "'");
                }
                
            }
            else if (request.getParameter("DOCTOR_PROFILE_CODE") != null) {
                doctorProfileRec = DBMgr.getRecord("SELECT * FROM DOCTOR_PROFILE WHERE CODE = '" + request.getParameter("DOCTOR_PROFILE_CODE") + "'");
            }

            //authentication
            /*
            Type of Doctor
            1	System Admin 	-> All

            2	Manager		->
                    - มีหน้าที่ตรวจสอบข้อมูลและทำการ Active รหัสแพทย์นั้นๆ
                    - ไม่สามารถแก้ไขข้อมูลทั้งหมดได้ยกเว้น Active
            3	Organize Doctor	->
                    - สามารถดูข้อมูลและเพิ่มเติมเปลี่ยนแปลงแก้ไขได้ทั้งหมด
                    - ไม่สามารถ Active รหัสแพทย์นั้นได้ (ในกรณีเพิ่มหรือแก้ไข ให้ Set เป็น InActive)
            4	DF User(IT)	->
                    - สามารถดูข้อมูลได้ทั้งหมดแต่ไม่สามารถทำการเปลี่ยนแปลงแก้ไข

            5	User(Doctor)	->
			
            
			แก้ใข : 
				1. DF User ดูข้อมูลแพทย์ได้ทั้งหมด และสามารถแก้ไขข้อมูลแพทย์ที่ Active เป็น 1,0 ได้ยกเว้น Active เป็น null 
				  และไม่สามารถแก้ไขข้อมูล Bank ได้
				
				2. Manager มีสิทธิ์เหมือน DF User แต่สามารถเรียกรายการที่ MAO แก้ไขมา Approve ได้ (Active เป็น null)
				
				3. MAO สามารถใช้งานได้ทุก Field ยกเว้น Actvie และเมื่อมีการเพิ่มรหัสแพทย์ระบบจะ Set Active = ''
				แต่เมื่อมีการแก้ไขข้อมูลทั่วไปที่ไม่ใช่ข้อมูล Bank ไม่ต้องมีการ Approve จาก Manager
				 แต่เมื่อมีการแก้ไขข้อมูล Bank จะต้องมีการ Approve จาก Manager
 
*/ 
            String readonlyManager = "";
            String disabledManager = "";
            String disabledDFUser = "";
            String disabledOrgDC = "";
            String disabledAdmin = "";
            //---- แก้ไข 2009-07-02
            String dfuserLoginDisabled = "";
            String dfuserLoginReadonly = "";
            String dfuserLoginSave = "";
            String dataActiveIsNullDfuser = "";
            
          	//---- แก้ไข 2009-08-10
          	String readonlyManagerV2 = "";
          	String disabledManagerV2 = "";
            if("1".equalsIgnoreCase(session.getAttribute("USER_GROUP_CODE").toString())){
            	/*
            	readonlyManager = "readonly=\"readonly\"";
                disabledManager = "disabled=\"disabled\"";
                disabledDFUser = "disabled=\"disabled\"";
                disabledOrgDC = "disabled=\"disabled\"";
                */
                disabledAdmin = "disabled=\"disabled\"";
            }else if("2".equalsIgnoreCase(session.getAttribute("USER_GROUP_CODE").toString())){
                //readonlyManager = "readonly=\"readonly\""; //20090710
                //disabledManager = "disabled=\"disabled\""; //20090710
                readonlyManagerV2 = "readonly=\"readonly\""; //20090710
                disabledManagerV2 = "disabled=\"disabled\""; //20090710                   
            }else if("3".equalsIgnoreCase(session.getAttribute("USER_GROUP_CODE").toString())){
                disabledOrgDC = "disabled=\"disabled\"";
                readonlyManager = "readonly=\"readonly\"";
            }else if("4".equalsIgnoreCase(session.getAttribute("USER_GROUP_CODE").toString())){
                //readonlyManager = "readonly=\"readonly\"";
                //disabledManager = "disabled=\"disabled\"";
                //disabledDFUser = "disabled=\"disabled\"";
                //disabledOrgDC = "disabled=\"disabled\"";
            	dfuserLoginDisabled = "disabled=\"disabled\"";
            	dfuserLoginReadonly = "readonly=\"readonly\"";
            	//out.println(DBMgr.getRecordValue(doctorRec, "ACTIVE"));
            	if("".equalsIgnoreCase(DBMgr.getRecordValue(doctorRec, "ACTIVE").toString()) || DBMgr.getRecordValue(doctorRec, "ACTIVE").toString() == null){
            		dfuserLoginSave = "disabled=\"disabled\"";
            		//return;
            	}
            }else{
            	readonlyManager = "readonly=\"readonly\"";
                disabledManager = "disabled=\"disabled\"";
                disabledOrgDC = "disabled=\"disabled\"";
            }
            //out.print("DBMgr.getRecordValue(doctorRec, IS_GUARANTEE_PROFILE) = " + DBMgr.getRecordValue(doctorRec, "IS_GUARANTEE_PROFILE"));
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>${labelMap.TITLE_MAIN}</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="../../css/share.css" media="all" />
        <link type="text/css" href="../../css/themes/custom-theme/jquery.ui.all.css" rel="stylesheet" media="screen"/>
        <script type="text/javascript" src="../../javascript/jquery-1.6.2.min.js"></script>
		<script type="text/javascript" src="../../javascript/jquery-ui-1.8.16.custom.min.js"></script>
        <script type="text/javascript" src="../../javascript/ajax.js"></script>
        <script type="text/javascript" src="../../javascript/util.js"></script>
        <script type="text/javascript" src="../../javascript/search_form.js"></script>
      
        <script type="text/javascript">
            function DOCTOR_CATEGORY_CODE_KeyPress(e) {
                var key = window.event ? window.event.keyCode : e.which;    // ? IE : Firefox

                if (key == 13) {
                    document.mainForm.DOCTOR_CATEGORY_CODE.blur();
                    return false;
                }
                else {
                    return true;
                }
            }

            function AJAX_Refresh_DOCTOR_CATEGORY() {
                var target = "../../RetrieveData?TABLE=DOCTOR_CATEGORY&COND=CODE='" + document.mainForm.DOCTOR_CATEGORY_CODE.value + "' AND HOSPITAL_CODE='<%=session.getAttribute("HOSPITAL_CODE")%>'";
                AJAX_Request(target, AJAX_Handle_Refresh_DOCTOR_CATEGORY);
            }
            
            function AJAX_Handle_Refresh_DOCTOR_CATEGORY() {
                if (AJAX_IsComplete()) {
                    var xmlDoc = AJAX.responseXML;

                    // Data not found
                    if (!isXMLNodeExist(xmlDoc, "CODE")) {
                        document.mainForm.DOCTOR_CATEGORY_CODE.value = "";
                        document.mainForm.DOCTOR_CATEGORY_DESCRIPTION.value = "";
                        return;
                    }
                    
                    // Data found
                    document.mainForm.DOCTOR_CATEGORY_DESCRIPTION.value = getXMLNodeValue(xmlDoc, "DESCRIPTION");
                }
            }
            
            function HOSPITAL_UNIT_CODE_KeyPress(e) {
                var key = window.event ? window.event.keyCode : e.which;    // ? IE : Firefox

                if (key == 13) {
                    document.mainForm.HOSPITAL_UNIT_CODE.blur();
                    return false;
                }
                else {
                    return true;
                }
            }

            function AJAX_Refresh_HOSPITAL_UNIT() {
                var target = "../../RetrieveData?TABLE=HOSPITAL_UNIT&COND=CODE='" + document.mainForm.HOSPITAL_UNIT_CODE.value + "'";
                AJAX_Request(target, AJAX_Handle_Refresh_HOSPITAL_UNIT);
            }
            
            function AJAX_Handle_Refresh_HOSPITAL_UNIT() {
                if (AJAX_IsComplete()) {
                    var xmlDoc = AJAX.responseXML;

                    // Data not found
                    if (!isXMLNodeExist(xmlDoc, "CODE")) {
                        document.mainForm.HOSPITAL_UNIT_CODE.value = "";
                        document.mainForm.HOSPITAL_UNIT_DESCRIPTION.value = "";
                        return;
                    }
                    
                    // Data found
                    document.mainForm.HOSPITAL_UNIT_DESCRIPTION.value = getXMLNodeValue(xmlDoc, "DESCRIPTION");
                }
            }
            
            function DEPARTMENT_CODE_KeyPress(e) {
                var key = window.event ? window.event.keyCode : e.which;    // ? IE : Firefox

                if (key == 13) {
                    document.mainForm.DEPARTMENT_CODE.blur();
                    return false;
                }
                else {
                    return true;
                }
            }

            function AJAX_Refresh_DEPARTMENT() {
                var target = "../../RetrieveData?TABLE=DEPARTMENT&COND=CODE='" + document.mainForm.DEPARTMENT_CODE.value + "'";
                AJAX_Request(target, AJAX_Handle_Refresh_DEPARTMENT);
            }
            
            function AJAX_Handle_Refresh_DEPARTMENT() {
                if (AJAX_IsComplete()) {
                    var xmlDoc = AJAX.responseXML;

                    // Data not found
                    if (!isXMLNodeExist(xmlDoc, "CODE")) {
                        //document.mainForm.DEPARTMENT_CODE.value = "";
                        document.mainForm.DEPARTMENT_DESCRIPTION.value = "";
                        return;
                    }
                    
                    // Data found
                    document.mainForm.DEPARTMENT_DESCRIPTION.value = getXMLNodeValue(xmlDoc, "DESCRIPTION");
                }
            }
            
            function BANK_CODE_KeyPress(e) {
                var key = window.event ? window.event.keyCode : e.which;    // ? IE : Firefox

                if (key == 13) {
                    document.mainForm.BANK_CODE.blur();
                    return false;
                }
                else {
                    return true;
                }
            }
            
            function AJAX_Refresh_BANK() {
                var target = "../../RetrieveData?TABLE=BANK&COND=CODE='" + document.mainForm.BANK_CODE.value + "'";
                AJAX_Request(target, AJAX_Handle_Refresh_BANK);
            }
            
            function AJAX_Handle_Refresh_BANK() {
                if (AJAX_IsComplete()) {
                    var xmlDoc = AJAX.responseXML;

                    // Data not found
                    if (!isXMLNodeExist(xmlDoc, "CODE")) {
                        //document.mainForm.BANK_CODE.value = "";
                        document.mainForm.BANK_DESCRIPTION.value = "";
                        //document.mainForm.SEARCH_BANK_BRANCH_CODE.disabled = false;
                        return;
                    }
                    
                    // Data found
                    document.mainForm.BANK_DESCRIPTION.value = getXMLNodeValue(xmlDoc, "DESCRIPTION_<%=labelMap.getFieldLangSuffix()%>");
                    //document.mainForm.SEARCH_BANK_BRANCH_CODE.disabled = true;

                    if(document.mainForm.BANK_BRANCH_CODE.value != ""){
                        AJAX_Refresh_BANK_BRANCH();
                    }
                }
            }
            
            function BANK_BRANCH_CODE_KeyPress(e) {
                var key = window.event ? window.event.keyCode : e.which;    // ? IE : Firefox

                if (key == 13) {
                    document.mainForm.BANK_BRANCH_CODE.blur();
                    return false;
                }
                else {
                    return true;
                }
            }
            
            function AJAX_Refresh_BANK_BRANCH() {
                var target = "../../RetrieveData?TABLE=BANK_BRANCH&COND=BANK_CODE='" + document.mainForm.BANK_CODE.value + "' AND CODE='" + document.mainForm.BANK_BRANCH_CODE.value + "'";
                AJAX_Request(target, AJAX_Handle_Refresh_BANK_BRANCH);
            }
            
            function AJAX_Handle_Refresh_BANK_BRANCH() {
                if (AJAX_IsComplete()) {
                    var xmlDoc = AJAX.responseXML;

                    // Data not found
                    if (!isXMLNodeExist(xmlDoc, "CODE")) {
                        document.mainForm.BANK_BRANCH_CODE.value = "";
                        document.mainForm.BANK_BRANCH_DESCRIPTION.value = "";
                        return;
                    }
                    
                    // Data found
                    document.mainForm.BANK_BRANCH_DESCRIPTION.value = getXMLNodeValue(xmlDoc, "DESCRIPTION_<%=labelMap.getFieldLangSuffix()%>");
                }
            }

            function AJAX_VerifyData() {
                var target = "../../RetrieveData?TABLE=DOCTOR&COND=CODE='" + document.mainForm.CODE.value + "' AND HOSPITAL_CODE='<%=session.getAttribute("HOSPITAL_CODE")%>'";
                AJAX_Request(target, AJAX_Handle_VerifyData);
            }
            
            function AJAX_Handle_VerifyData() {
                if (AJAX_IsComplete()) {
                    var xmlDoc = AJAX.responseXML;

                    var beExist = isXMLNodeExist(xmlDoc, "CODE");
                    
                    switch (document.mainForm.MODE.value) {
                        case "<%=DBMgr.MODE_INSERT%>" :
                            if (beExist) {
                                if (confirm("<%=labelMap.get("CONFIRM_REPLACE_DATA")%>")) {
                                document.mainForm.MODE.value= "<%=DBMgr.MODE_UPDATE%>";
                                document.mainForm.submit();
                            }
                        }
                        else {
                            document.mainForm.submit();
                        }
                        break;
                    case "<%=DBMgr.MODE_UPDATE%>" :
                        if (beExist) {
                            document.mainForm.submit();
                        }
                        else {
                            alert(beExist);
                            alert("<%=labelMap.get(LabelMap.ALERT_DATA_NOT_FOUND)%>");
                        }
                        break;
                    }
                }
            }

            function SAVE_Click() {
			
					if (!isObjectEmptyString(document.mainForm.CODE, '<%=labelMap.get(LabelMap.ALERT_REQUIRED_FIELD)%>') && 
						!isObjectEmptyString(document.mainForm.DOCTOR_PROFILE_CODE, '<%=labelMap.get(LabelMap.ALERT_REQUIRED_FIELD)%>')) {
					AJAX_VerifyData();
					}
			}

		
			  
			
        </script>
                <style type="text/css">
<!--
.style1 {color: #003399}
-->
        </style>

</head>
<body>
        <form id="mainForm" name="mainForm" method="post" action="#">
            <input type="hidden" id="MODE" name="MODE" value="<%= MODE %>" />
            <input type="hidden" id="IS_HOLD" name="IS_HOLD" value="N" />
            <input type="hidden" id="hospitalCode" name="hospitalCode" value="<%= session.getAttribute("HOSPITAL_CODE") %>" />
            <table class="form">
                <tr>
                    <th colspan="4" class="buttonBar">
                    	<div style="float: left;">${labelMap.TITLE_MAIN}</div>
						<% session.getAttribute("USER_GROUP_CODE").toString().equalsIgnoreCase("0"); %>
                        <input type="button" id="SAVE" name="SAVE" <%=dfuserLoginSave%> class="button" value="${labelMap.SAVE}" onclick="SAVE_Click()" />
                        <input type="reset" id="RESET" name="RESET" class="button" value="${labelMap.RESET}" />
                        <input type="button" id="CLOSE" name="CLOSE" class="button" value="${labelMap.CLOSE}" onclick="window.location.href='doctor_mao_setup.jsp?CODE=<%=request.getParameter("DOCTOR_PROFILE_CODE")%>'" />                    </th>
				</tr>
                <tr>
                    <td class="label">
                        <label for="DOCTOR_PROFILE_CODE"><span class="style1">${labelMap.DOCTOR_PROFILE_CODE}*</span></label>                    </td>
                    <td class="input" colspan="3">
                    	<input type="text" id="DOCTOR_PROFILE_CODE" name="DOCTOR_PROFILE_CODE" class="short" maxlength="20" readonly="readonly" value="<%= DBMgr.getRecordValue(doctorProfileRec, "CODE")%>" />
                        <input type="text" id="DOCTOR_PROFILE_NAME" name="DOCTOR_PROFILE_NAME" class="long" readonly="readonly" value="<%= DBMgr.getRecordValue(doctorProfileRec, "NAME") %>" />                    </td>
                </tr>
                <tr>
                  <td class="label"> 
                    <label for="CODE"><span class="style1">${labelMap.CODE}*</span></label>                    </td>
                    <td class="input">
                        <input name="CODE" type="text" class="short" id="CODE" value="<%= DBMgr.getRecordValue(doctorRec, "CODE")%>" maxlength="50"   <% if(MODE==DBMgr.MODE_UPDATE)out.print("readonly=\"readonly\"");%> />                    </td>
                    <td class="label">
                        <label for="ACTIVE_1">${labelMap.ACTIVE}</label>                    </td>
                    <td class="input">
                        <input type="radio" id="ACTIVE_1" name="ACTIVE" value="1"<%= DBMgr.getRecordValue(doctorRec, "ACTIVE").equalsIgnoreCase("1") ? " checked=\"checked\"" : ""%> <%=dfuserLoginDisabled%> <%=disabledOrgDC %> <%=disabledAdmin %>/>
                        <label for="ACTIVE_1">${labelMap.ACTIVE_1}</label>
                        <input type="radio" id="ACTIVE_0" name="ACTIVE" value="0"<%= DBMgr.getRecordValue(doctorRec, "ACTIVE").equalsIgnoreCase("0") ? " checked=\"checked\"" : ""%> <%=dfuserLoginDisabled%> <%=disabledOrgDC %> <%=disabledAdmin %>/>
                        <label for="ACTIVE_0">${labelMap.ACTIVE_0}</label>                    </td>
                </tr>
                <tr>
                    <td class="label">
                        <label for="NAME_ENG"><span class="style1">${labelMap.NAME_ENG}*</span></label>                    </td>
                    <td class="input" colspan="3">
                        <input name="NAME_ENG" type="text" class="long" id="NAME_ENG" value="<%= DBMgr.getRecordValue(doctorRec, "NAME_ENG").equals("") ? DBMgr.getRecordValue(doctorProfileRec, "NAME_ENG") : DBMgr.getRecordValue(doctorRec, "NAME_ENG") %>" maxlength="255" />                    </td>
                </tr>
                <tr>
                    <td class="label">
                        <label for="NAME_THAI"><span class="style1">${labelMap.NAME_THAI}*</span></label>                    </td>
                    <td class="input" colspan="3">
                        <input name="NAME_THAI" type="text" class="long" id="NAME_THAI" value="<%= DBMgr.getRecordValue(doctorRec, "NAME_THAI").equals("") ? DBMgr.getRecordValue(doctorProfileRec, "NAME_THAI") : DBMgr.getRecordValue(doctorRec, "NAME_THAI") %>" maxlength="255" />					</td>
                </tr>
                <tr><th colspan="4">${labelMap.SUBTITLE_ADDRESS}</th></tr>
                <tr>
                    <td class="label">
                        <label for="ADDRESS1">${labelMap.ADDRESS1}</label>                    </td>
                    <td class="input" colspan="3">
                        <input name="ADDRESS1" type="text" class="long" id="ADDRESS1" value="<%= DBMgr.getRecordValue(doctorRec, "ADDRESS1")%>" maxlength="255" />                    </td>
                </tr>
                <tr>
                    <td class="label">
                        <label for="ADDRESS2">${labelMap.ADDRESS2}</label>                    </td>
                    <td class="input" colspan="3">
                        <input name="ADDRESS2" type="text" class="long" id="ADDRESS2" value="<%= DBMgr.getRecordValue(doctorRec, "ADDRESS2")%>" maxlength="255" />                    </td>
                </tr>
                <tr>
                    <td class="label">
                        <label for="ADDRESS3">${labelMap.ADDRESS3}</label>                    </td>
                    <td class="input" colspan="3">
                        <input name="ADDRESS3" type="text" class="long" id="ADDRESS3" value="<%= DBMgr.getRecordValue(doctorRec, "ADDRESS3")%>" maxlength="255" />                    </td>
                </tr>
                <tr>
                    <td class="label">
                        <label for="ZIP">${labelMap.ZIP}</label>                    </td>
                    <td class="input" colspan="3">
                        <input name="ZIP" type="text" class="short" id="ZIP" value="<%= DBMgr.getRecordValue(doctorRec, "ZIP")%>" maxlength="50" />                    </td>
                </tr>
                <tr>
                    <td class="label">
                        <label for="EMAIL">${labelMap.EMAIL}</label>                    </td>
                    <td class="input" colspan="3">
                        <input name="EMAIL" type="text" class="long" id="EMAIL" value="<%= DBMgr.getRecordValue(doctorRec, "EMAIL")%>" maxlength="50" />                    </td>

                <tr>
                    <th colspan="4" class="buttonBar">
						<% session.getAttribute("USER_GROUP_CODE").toString().equalsIgnoreCase("0"); %>
                        <input type="button" id="SAVE" name="SAVE" <%=dfuserLoginSave%> class="button" value="${labelMap.SAVE}" onclick="SAVE_Click()" />
                        <input type="reset" id="RESET" name="RESET" class="button" value="${labelMap.RESET}" />
                        <input type="button" id="CLOSE" name="CLOSE" class="button" value="${labelMap.CLOSE}" onclick="window.location.href='doctor_mao_setup.jsp?CODE=<%=request.getParameter("DOCTOR_PROFILE_CODE")%>'" />                    </th>
                </tr>
            </table>
   
        </form>
    </body>
</html>


